package com.inossem.warehousestorage_inossem.listener;

import android.content.Context;
import android.content.Intent;

import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.UtilsConstant;
import com.inossem.warehousestorage_inossem.activity.LoginActivity;
import com.inossem.warehousestorage_inossem.application.WarehouseStorageApplication;
import com.inossem.warehousestorage_inossem.model.StringResponse;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

/**
 * Created by leij on 2017/6/5.
 */

public abstract class BaseStringRequestListener implements RequestListener<StringResponse> {

    @Override
    public void onDefaultError(Context context, StringResponse stringRequestResponse) {
        //默认错误处理
        if (UtilsConstant.REQUEST_RESULT_CODE_401.equals(stringRequestResponse.getResponseCode())) {
            SPUtils.saveLoginFlag(context, false);
            WarehouseStorageApplication.getInstance().removeAllActivity();
            Intent intent = new Intent(context, LoginActivity.class);
            context.startActivity(intent);
        } else {
            Utils.middleToast(context, "系统异常,服务器返回响应码：" + stringRequestResponse.getResponseCode());
        }
    }

    @Override
    public void onError(Context context, StringResponse stringRequestResponse) {
        //特殊错误处理，需要先复写useDefaultError方法将返回值改成false才会生效。
    }

    @Override
    public abstract void onSuccess(StringResponse stringRequestResponse);

    // 如果想让onError方法生效必须复写此方法，并且返回值为false
    @Override
    public boolean useDefaultError() {
        return true;
    }

}
